/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.entities;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author huijia
 */
public class CalculateXY {
    private List<xObject> xList = new ArrayList<xObject>();
    private xObject currXObj = null;
    private yObject currYObj = null;
    private boolean isFirst = true;
    private long currXTime = 0;
    private long currYTime = 0;
    
    public List<xObject> process(List<DBParameter> paramList, HealthStatus hs, String paramType)
    {
        long tmpTime =0;
        
        Iterator<DBParameter> spIter = paramList.iterator();
        while (spIter.hasNext())
        {
            DBParameter paramObj = spIter.next();
            if (isFirst==true)
            {
                currXTime = paramObj.getdateTime().getTime();
                currXObj = new xObject();
                DBParameter tmpXObj = DBParamFactory.getDBParameter(paramType);
                tmpXObj.setdateTime(new Timestamp(currXTime));
                currXObj.setxObj(tmpXObj);

                currYObj = new yObject();
                DBParameter tmpYObj = DBParamFactory.getDBParameter(paramType);
                currYTime = currXTime;
                tmpYObj.setdateTime(new Timestamp(currYTime));
                currYObj.setyObj(tmpYObj);

                currYObj.addDBParameter(paramObj);
                isFirst = false;
            }
            
            else
            {
                tmpTime = paramObj.getdateTime().getTime();
                if (tmpTime < currXTime + hs.getXBlock())
                {
                    processY (paramObj,hs, paramType);
                } // if (newTime < currTime + hs.getXBlock())
                
                else
                {
                    currXObj.addYObject(currYObj);
                    
                    while (tmpTime >= currXTime + hs.getXBlock())
                    {
                        xList.add(currXObj);
                        currXTime = currXTime + hs.getXBlock();
                        currXObj = new xObject();
                        DBParameter tmpXObj = DBParamFactory.getDBParameter(paramType);
                        tmpXObj.setdateTime(new Timestamp(currXTime));
                        currXObj.setxObj(tmpXObj);
                    }

                    currYObj = new yObject();
                    DBParameter tmpYObj = DBParamFactory.getDBParameter(paramType);
                    currYTime = currXTime;
                    tmpYObj.setdateTime(new Timestamp(currYTime));
                    currYObj.setyObj(tmpYObj);
                        
                    processY (paramObj, hs, paramType);
                }
            } // else
        } // while
        currXObj.addYObject(currYObj);
        xList.add(currXObj);
        
        return xList;
    }
    
    private void processY (DBParameter paramObj, HealthStatus hs, String paramType) {
        if (paramObj.getdateTime().getTime() < currYTime + hs.getYBlock())
        {
            currYObj.addDBParameter(paramObj);
        }
        else
        {
            while (paramObj.getdateTime().getTime()>= currYTime + hs.getYBlock())
            {
                currXObj.addYObject(currYObj);
                currYObj = new yObject();
                DBParameter tmpYObj = DBParamFactory.getDBParameter(paramType);
                currYTime = currYTime + hs.getYBlock();
                tmpYObj.setdateTime(new Timestamp(currYTime));
                currYObj.setyObj(tmpYObj);
            }

            currYObj.addDBParameter(paramObj);
        }
    }
}
